/**
 * C Programming: A Modern Approach
 *
 * version: 2nd
 * author: K.N.King
 * http://knking.com/books/c2
 */

#include <stdio.h>

/**
 *
 */

int main(void) {
    int numbers[16];

    // get numbers 1 to 16
    printf("Enter the numbers from 1 to 16 in any order:\n");
    for (int i = 0; i < 16; ++i) {
        scanf("%d", &numbers[i]);
    }

    printf("\n");

    // print numbers
    for (int i = 1; i <= 16; ++i) {
        printf("%2d  ", numbers[i - 1]);
        if (i % 4 == 0) {
            printf("\n");
        }
    }

    // calculate row sum
    int row1_sum = numbers[0] + numbers[1] + numbers[2] + numbers[3];
    int row2_sum = numbers[4] + numbers[5] + numbers[6] + numbers[7];
    int row3_sum = numbers[8] + numbers[9] + numbers[10] + numbers[11];
    int row4_sum = numbers[12] + numbers[13] + numbers[14] + numbers[15];
    printf("\nRow sums: %d %d %d %d", row1_sum, row2_sum, row3_sum, row4_sum);

    // calculate column sums
    int column1_sum = numbers[0] + numbers[4] + numbers[8] + numbers[12];
    int column2_sum = numbers[1] + numbers[5] + numbers[9] + numbers[13];
    int column3_sum = numbers[2] + numbers[6] + numbers[10] + numbers[14];
    int column4_sum = numbers[3] + numbers[7] + numbers[11] + numbers[15];
    printf("\nColumn sums: %d %d %d %d", column1_sum, column2_sum, column3_sum,
           column4_sum);

    // calculate diagonal sums
    int diagonal_sum1 = numbers[0] + numbers[5] + numbers[10] + numbers[15];
    int diagonal_sum2 = numbers[3] + numbers[6] + numbers[9] + numbers[12];
    printf("\nDiagonal sums: %d %d\n", diagonal_sum1, diagonal_sum2);

    return 0;
}

/*
output:
Enter the numbers from 1 to 16 in any order:
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1

16   3   2  13
 5  10  11   8
 9   6   7  12
 4  15  14   1

Row sums: 34 34 34 34
Column sums: 34 34 34 34
Diagonal sums: 34 34
*/